package com.xszx.serviceedu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xszx.commonutils.R;
import com.xszx.serviceedu.entity.Course;
import com.xszx.serviceedu.entity.Teacher;
import com.xszx.serviceedu.entity.query.TeacherQuery;
import com.xszx.serviceedu.service.CourseService;
import com.xszx.serviceedu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 白熊
 * @since 2024-07-08
 */
@Api(tags = "讲师管理")//Swagger用在类上
//@CrossOrigin//解决跨域问题
@RestController
@RequestMapping("/serviceedu/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "分页讲师列表")
    @GetMapping(value = "{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        Page<Teacher> pageParam = new Page<Teacher>(page, limit);

        Map<String, Object> map = teacherService.pageListWeb(pageParam);

        return  R.ok().data(map);
    }

    @PutMapping("{id}")//根据id修改教师信息
    @ApiOperation("根据id修改教师信息")
    public R update(@PathVariable @ApiParam(value = "对象id",required = true)  String id ,
                    @RequestBody @ApiParam(value = "教师对象",required = true) Teacher teacher){
        teacher.setId(id);//这个是将修改的id用户
        teacherService.updateById(teacher);
        return R.ok().message("修改成功");
    }

    @GetMapping("{id}")//根据id获取教师
    @ApiOperation("根据id查询教师")
    public R getById(@ApiParam(value = "对象id",required = true,name = "id")
                         @PathVariable("id") String id){
        Teacher teacher = teacherService.getById(id);
        List<Course> courseList = courseService.selectByTeacherId(id);
        return R.ok().data("item",teacher).data("courseList",courseList);
    }
    @ApiOperation("添加教师")//添加教师信息
    @PostMapping//添加就是前端传过来一个teacher对象 传过来的是json对象  需要@RequestBody注解解析
    public R add(@RequestBody
                     @ApiParam(value = "教师对象",required = true,name = "teacher") Teacher teacher){
        teacherService.save(teacher);
        return R.ok().message("添加成功");
    }

    @PostMapping("{page}/{limit}")
    @ApiOperation("分页模糊查询教师")//分页模糊查询教师信息
    public R query(@PathVariable @ApiParam(value = "当前页数",required = true) Long page,
                   @PathVariable @ApiParam(value = "每页条数",required = true) Long limit,
                   @RequestBody @ApiParam("模糊查询") TeacherQuery teacherQuery){

        Page<Teacher> pageParam = new Page<>(page,limit);
        teacherService.pageQuery(pageParam,teacherQuery);
        List<Teacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total",total)
                .data("records",records);

    }


    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id删除老师")
    public R de(@PathVariable @ApiParam("用户对象") String id){
        boolean result = teacherService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }
    @GetMapping
    @ApiOperation(value = "查询所有教师")
    public R list(){
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    //删除测试
//    @DeleteMapping("{id}")
//    @ApiOperation(value = "根据id删除用户")
//    public boolean de(@PathVariable @ApiParam("用户对象") String id){
//        return teacherService.removeById(id);
//    }
//    @GetMapping
//    public List<Teacher> list(){
//        return teacherService.list(null);
//    }


    //    @GetMapping("{page}/{limit}")
//    @ApiOperation(value = "分页查询教师信息")
//    public R page(@PathVariable @ApiParam(value = "页数",required = true) Long page,
//                  @PathVariable @ApiParam(value = "每页条数",required = true) Long limit){
//        Page<Teacher> pageParm = new Page<>(page,limit);
//        teacherService.page(pageParm,null);
//        //从pageParm对象中获取当前页的记录列表records和总记录数total。
//        //传给前端数据
//        List<Teacher> records = pageParm.getRecords();
//        long total = pageParm.getTotal();
//        return R.ok().data("total",total)
//                .data("records",records);
//    }
}

